﻿using JavaScriptEngineSwitcher.V8;

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace underMine.save.windbell2
{
    //https://undermine.gamepedia.com/Template:GetID
    class viewModel: INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this,
                new PropertyChangedEventArgs(propertyName));
            }
        }
        public bool isOk { get; set; } = false;
        public List<圣物> items { get; set; }
        public string savePath { get; set; } = "";
        public int playerType { get; set; } = 0;
        public V8JsEngine v8engine = new V8JsEngine();
        public viewModel()
        {
            getSavePath();
            if (!File.Exists(savePath))
            {
                this.isOk = true;
            }
            readConfig();
            v8engine.ExecuteFile(AppDomain.CurrentDomain.BaseDirectory + "process.js");
        }
        public void getSavePath()
        {
            this.savePath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + @"\AppData\LocalLow\Thorium Entertainment\UnderMine\Saves\Save0.json";
        }
        public string read()
        {
            return File.ReadAllText(savePath);
        }
        public void readConfig()
        {

            var path = AppDomain.CurrentDomain.BaseDirectory + "template.json";
            var res = JToken.Parse(File.ReadAllText(path));
            items = res["items"].ToObject<List<圣物>>();


        }
        public void 加圣物()
        {
            var saveContent = v8engine.CallFunction("pushItems",read(), JsonConvert.SerializeObject( items.Where(d=>d.selected)), playerType);
            
            save(saveContent.ToString());
            
        }

        public void 加炸弹()
        {

            var saveContent = v8engine.CallFunction("fillBombKey",read(),  playerType);

            save(saveContent.ToString());
        }
        public void save(string saveContent)
        {
            File.WriteAllText(savePath, saveContent);
        }
    }
}
